/*
 * @Author: sunzhijun 
 * @Date: 2020-02-28 21:36:16 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2020-03-26 10:06:08
 */


import CommonAction from './common';
import { base, promptBox } from 'nc-lightapp-front';
import {chectFormAction} from '../../../public/functions/checkFormAction';
export default class CardAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    // 获取卡片内容数据
    getFormData = async ( isRefresh )=>{
        const { props, action } = this.comp;
        const { main, form, button } = props;
        let { appConfig, pk_org, pk_ctrtmng, areaCode, cardId, fromApprove, language, isModifyEdit } = main;
        action.btnAct.buttonStatus();
        let res = await this.dispatch({
           type: 'main/getFormAction',
           payload: {
                pk_org: pk_org,
                appCode: appConfig.appcode,
                pageCode: appConfig.pagecode,
                areaCode: areaCode,
                pk_ctrtmng: pk_ctrtmng
           }
        });
        if( res.success ){
            let data = res.data;
            if(Object.keys(data).length === 0){
                button.setButtonDisabled({
                    edit: true,
                    // add: true,
                    submit: true,
                    delete: true,
                    launch: true,
                    // history: true,
                    // revoke: true,
                    // refresh: true,
                    // export_excel: true
                });
                form.EmptyAllFormValue(cardId);
            }else{
                let formData = data.detailForm[cardId];
                let approve_state = formData.rows[0].values.approve_state.value;
                button.setButtonDisabled({
                    edit: false,
                    // add: true,
                    submit: false,
                    delete: false,
                    launch: false,
                    // history: true,
                    // revoke: true,
                    // refresh: true,
                    // export_excel: true
                });
                if(!isModifyEdit){ //如果为真那么是从列表行编辑进来
                    if( fromApprove ){ //来自审批中心的进行中和提交态
                        if( approve_state === '2' || approve_state === '3' ){
                            button.setButtonVisible({
                                edit: true,
                                add: false,
                                submit: false,
                                query: false,
                                delete: false,
                                launch: false,
                                history: false,
                                revoke: false,
                                refresh: false,
                                export_excel: false,
                                cardview: true,
                                file: true
                            });
                        }else{
                            button.setButtonVisible(['add', 'edit', 'submit', 'query', 'delete', 'launch', 'history', 'revoke', 'refresh','export_excel', 'file'], false);
                        }
                    }else{
                        //根据单据状态判断肩部按钮的显示
                        if(approve_state === '-1'){ //自由态
                            button.setButtonVisible(['add', 'edit', 'submit', 'delete', 'refresh', 'cardview'], true);
                            button.setButtonVisible([ 'history', 'query', 'revoke', 'export_excel' ], false);
                        }else if(approve_state === '2' || approve_state === '3'){ //进行中和提交态
                            button.setButtonVisible(['add', 'history', 'revoke', 'refresh', 'cardview'], true);
                            button.setButtonVisible(['edit', 'query', 'submit', 'delete', 'export_excel'], false);
                        }else if(approve_state === '0' || approve_state === '1'){
                            button.setButtonVisible(['add', 'history', 'refresh', 'cardview'], true);
                            button.setButtonVisible(['edit', 'revoke', 'query', 'submit', 'delete', 'export_excel'], false);
                        }
                        // //判断是否电子合同且状态为4时显示发起操作
                        // if( signtype === '2' && sign_state === '4'){
                        //     button.setButtonVisible(['launch'], true);
                        // }else{
                        //     button.setButtonVisible(['launch'], false);
                        // }
                        button.setButtonVisible(['file'], true)
                    }
                }
                form.setAllFormValue({[cardId]: formData});
                this.checkoutCardData('', cardId, props, [cardId]);
            }
            action.headerAct.setCardPagination(pk_ctrtmng);
            if (!!isRefresh && isRefresh === 'refresh') {
                this.toast({ color: 'success', content: language['hrcm-000016']}); /*国际化：刷新成功！*/
            }
        }
    }

    // 编辑前校验
    checkDataAction = async ()=> {
        const { props, action } = this.comp;
        const { main, form } = props;
        let { appConfig, pk_org, pk_ctrtmng } = main;
        let res = await this.dispatch({
           type: 'main/checkDataAction',
           payload: {
                // pk_org: pk_org,
                appCode: appConfig.appcode,
                billPks: pk_ctrtmng,
                opFlag: 'edit'
           }
        });
        if( res.success ){
            form.setFormStatus('form', 'edit');
            action.btnAct.editButtonStatus();
        }
    }

    // 取消
    cancelAction = ()=>{
        const { props, action } = this.comp;
        const { main, form, dispatch } = props;
        form.setFormStatus('form', 'browse');
        action.btnAct.initButtonStatus();
    }

    // 保存
    saveDataAction = async ()=>{
        const { props, action } = this.comp;
        const { main, form, dispatch } = props;
        let { appConfig, pk_org, pk_ctrtmng, step, info2, language } = main;
        let allFormValues = form.getAllFormValue('form');
        let dataObj = {
            baseParam: {step: step},
            formData: {
                model: allFormValues
            }
        }
        let res = await this.dispatch({
           type: 'main/saveDataAction',
           payload: {
                ...dataObj
           }
        });
        if( res.success ){
            let data = res.data;
            if(!!data){
                dispatch({
                    type: 'main/update',
                    payload: {
                        step: res.data.step,
                        info2: res.data.info2
                    }
                })
                let title = language['hrcm-000041']; // 确认继续
                if (res.data.step == '1') {
                    this.saveAfterTips(res.data.msg, title)
                } else {
                    toast({
                        color: 'success',
                        content: language['hrcm-000045'] // 保存成功
                    })
                    form.setFormStatus('form', 'browse');
                    action.btnAct.initButtonStatus();
                    dispatch({
                        type: 'main/update',
                        payload: {
                            step: '0'
                        }
                    })
                }
            }
        }
    }

    // 点击修改保存成功之后的提示
    saveAfterTips = (message, title)=> {
        let { props: { main: { language } } } = this.comp;
        promptBox({
            color: 'warning',
            title: title, // 确认继续
            content: message,
            noFooter: false,
            noCancelBtn: false,
            beSureBtnName: language['hrcm-000018'], // 是
            cancelBtnName: language['hrcm-000019'], // 否
            beSureBtnClick: this.saveDataAction,
            hasCloseBtn:false,
            cancelBtnClick: this.cancelBtnClick
        })
    }

    // 点击取消按钮
    cancelBtnClick () {
        const { props } = this.comp;
        const { main, form, dispatch } = props;
        dispatch({
            type: 'main/update',
            payload: {
                step: '0'
            }
        })
    }
    // 表格编辑后事件
    formAfterEventFn = (props,moduleId,key,value,oldValue)=>{
        this.checkoutCardData(key , moduleId, props, [moduleId]);
    }

    //卡片数据编辑前校对
    checkoutCardData = (key, moduleId, props, moduleIdArr)=>{
        let { main } = props;
        let { appConfig: { appcode } } = main;
        let url = '/nccloud/hrcm/ctrtmng/CtrtMngFormAfterEditAction.do';
        chectFormAction(key, moduleId, props, moduleIdArr, appcode, '', url);
    }
}